package cn.zyk.config;


import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.YamlMapFactoryBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import java.util.HashMap;
import java.util.Map;

/*
 * 资源路径配置：加载<resourcepath.yml>中的自定义配置信息
 * @Author zangyk
 * @Date 2025/5/19
 */

@Configuration
@Slf4j
@Data
public class ResourcePathConfig {

    //定义全局属性
    public String driver;//网格图userinfo接口
    public String url;
    public String username;
    public String password;


    @PostConstruct
    public void getResourcePathMap() {
        //1.通过YamlMapFactoryBean读取yml文件 返回一个map
        YamlMapFactoryBean yamlMapFactoryBean = new YamlMapFactoryBean();
        yamlMapFactoryBean.setResources(new ClassPathResource("resourcepath.yml"));
        //2.全日志源配置：dev、test、prod
        Map<String, Object> sources = yamlMapFactoryBean.getObject();

        //3.读取当前环境的资源路径配置
        Map<String, Object> conf = (HashMap<String, Object>) sources.get("spring");
        HashMap<String, String> datasource = (HashMap<String, String>) conf.get("datasource");
        driver = datasource.get("driver-class-name");
        url = datasource.get("url");
        username = datasource.get("username");
        password = datasource.get("password");


    }

}

